<?=view('header.html');?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">
                <div class="layui-card-header">
                    菜单管理
                </div>
                <div class="layui-card-body">
                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOption">
                        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="toolbar_display">排序</button>
                        <button type="button" lay-event="toolbar_del" title="您确定要这样操作吗？" class="layui-btn layui-btn-danger layui-btn-sm">删除</button>
                        <button class="layui-btn layui-btn-blue layui-btn-sm" lay-submit lay-filter="menu_add">添加菜单</button>
                    </script>

                    <script type="text/html" id="showTpl">
                        <input type="checkbox" name="show" value="{{d.show}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="showSwitch" {{ d.show == 1 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="barOption">
                        {{# if (d.pid==0){ }}
                        <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
                        {{# } }}
                        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html');?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;

            /**index**/

            //数据初始化
            var tableInit = table.render({
                elem: '#listTable',
                toolbar: "#toolbarOption",
                title: '菜单列表',
                url: adminpath + 'admin/menu/index',
                method: 'post',
                cols: [
                    [
                        { type: 'checkbox' },
                        { field: 'id', title: 'ID', width: 80, unresize: true, sort: true },
                        {
                            field: 'displayorder',
                            width: 80,
                            title: '排序',
                            edit: 'text'
                        },
                        {
                            field: 'name',
                            width: 200,
                            title: '名称',
                            templet: function (d) {
                                return d.spacer + '<i class="layui-icon ' + d.icon + '"></i> ' + d.name
                            }
                        },
                        { field: 'uri', title: 'URI' },
                        { field: 'show', title: '显示', templet: '#showTpl', unresize: true },
                        { title: '操作', toolbar: '#barOption' }
                    ]
                ],
                page: true, limits: [1, 2, 3, 4, 5, 10],
                limit: 1,
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        "data": res.data.data,
                        'count': res.data.total
                    };
                }
            });


            form.on('switch(menuHidden)', function (obj) {
                layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            });


            //监听单元格编辑
            table.on('edit(listTable)', function (obj) {
                var value = obj.value //得到修改后的值
                    , data = obj.data //得到所在行所有键值
                    , field = obj.field; //得到字段

                switch (field) {
                    case 'displayorder':
                        layer.confirm('确定要排序？', function (index) {
                            $.post(adminpath + 'admin/menu/displayorder', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg('操作成功', { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;
                }

            });

            //表头工具栏
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                })

                if (ids.length < 1) {
                    layer.msg('您还没有选择', { icon: 5, anim: 6 });
                    return false;
                }

                switch (obj.event) {

                    case 'toolbar_del':
                        layer.confirm('确定要删除这些？', function (index) {
                            $.post(adminpath + 'admin/menu/del', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg('操作成功', { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;

                };
            });

            //监听表格行操作
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id

                switch (obj.event) {

                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['600px', '80%'],
                            title: '添加菜单',
                            scrollbar: false,
                            // maxmin: true,
                            content: [adminpath + 'admin/menu/add?pid=' + id, 'yes'],
                            success: function (layero, index) {
                                layer.iframeAuto(index);
                            }
                        });
                        break;

                    case 'edit':
                        layer.open({
                            type: 2,
                            area: ['600px', '80%'],
                            title: '修改菜单',
                            scrollbar: false,
                            content: [adminpath + 'admin/menu/edit?id=' + id, 'yes'],
                            success: function (layero, index) {
                                layer.iframeAuto(index);
                            }
                        });
                        break;

                    case 'del':
                        layer.confirm('确定要删除吗？', function (index) {

                            $.post(adminpath + 'admin/menu/del', { ids: [id] }, function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg('操作成功', { icon: 1, time: 1500 }, function () {
                                        tableInit.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6, time: 1500 });
                                }
                                layer.closeAll();
                            }, 'json');
                        });

                        break;
                }
            });

            //监听状态操作
            form.on('switch(showSwitch)', function (obj) {
                var id = obj.othis.parents('tr').find("td:eq(1)").find(".layui-table-cell").text();
                var title = '';
                var val = '';
                if (this.value == 1) {
                    title = '您确定要设为隐藏吗?';
                    val = 0;
                } else {
                    title = '您确定要设为显示吗?';
                    val = 1;
                }

                //点确认再切换
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render('checkbox');

                layer.confirm(title, function (index) {
                    $.post(adminpath + "admin/menu/switchHidden", { id: id, value: val }, function (res) {
                        if (res.code == 0) {
                            layer.msg('操作成功', { icon: 1, time: 1500 });
                        } else {
                            layer.msg(res.msg, { icon: 5, anim: 6 });
                        }
                        tableInit.reload();
                    }, 'json');
                    layer.close(index);
                });
            });

            //搜索条件

            //搜索店铺
            form.on('radio(searchShop)', function (data) {
                console.log(data.value);
                var shopid = data.value;
                tableInit.reload({
                    where: { shopid: shopid },
                    page: {
                        curr: 1
                    }
                });
            });

            //标题搜索
            form.on('submit(searchTitle)', function (data) {
                var value = data.field['title'];
                tableInit.reload({
                    where: { title: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //搜索站点
            form.on('radio(searchSite)', function (data) {
                var value = data.value;
                tableInit.reload({
                    where: { placecode: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //上下架
            form.on('radio(searchOnsale)', function (data) {
                var value = data.value;
                tableInit.reload({
                    where: { onsale: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //审核
            form.on('radio(searchStatus)', function (data) {
                var value = data.value;
                tableInit.reload({
                    where: { status: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //发布状态
            form.on('radio(searchSubmitStatus)', function (data) {
                var value = data.value;
                tableInit.reload({
                    where: { submitstatus: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //***********add*********//

            //选择店铺 加载授权列表
            form.on('select(selectAshop)', function (data) {
                console.log(data);
                var id = data.value;
                var url = '/index.php?s=member&c=product&m=get_ashop_oauth&id=' + id;
                $.get(url, function (res) {
                    if (res.code == 0) {
                        if (res.data.length >= 1) {
                            var str = '<input type="checkbox" value="all" title="全选" lay-filter="allSite">';
                            $.each(res.data, function (i, v) {
                                str += '<input type="checkbox" class="sons" name="data[placecode][]" value="' + v.placecode + '" title="' + v.cnname + '"/>';
                            });
                            $("#placearea").html(str);
                            form.render();
                        }
                    } else {
                        $("#placearea").html('');
                        form.render();
                        layer.msg(res.msg);
                    }
                }, 'json');
            });

            //全选店铺
            form.on('checkbox(allSite)', function (data) {
                if (data.elem.checked == true) {
                    $("#placearea .sons").prop("checked", true);
                } else {
                    $("#placearea .sons").prop("checked", false);
                }
                form.render('checkbox');
            });

            form.on('submit(menu_add)', function () {
                layer.open({
                    type: 2,
                    // skin: 'layui-layer-lan',
                    area: ['600px', '80%'],
                    title: '添加/修改菜单',
                    content: [adminpath + 'admin/menu/add', 'no']
                });
                return false;
            });

            //搜索条件
            form.on('submit(ams_reset)', function (data) {
                $("#searchForm")[0].reset();
                form.render();
                $('#ams_search').trigger('click');
            });

            //标题搜索
            form.on('submit(ams_search)', function (data) {
                // console.log(data.field);return;
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });

        })
    </script>
</body>

</html>